<template>
  <div id="app">
    <!-- Header -->
    <header class="header can-sticky">
      <el-row type="flex" justify="space-between" align="middle" class="container">
        <el-col :span="4">
          <div class="logo">
            <a href="/home">
              <img src="@/assets/images/logo.png" alt="logo" />
            </a>
          </div>
        </el-col>
        <el-col :span="16">
          <nav class="navigation">
            <ul class="main-menu">
              <li
                v-for="(item, index) in mainMenu"
                :key="index"
                class="menu-item"
                :class="{ 'menu-item-has-children': !!item.children }"
              >
                <a :href="item.href" @click="handleMenuClick(item, $event)">
                  {{ item.title }}
                </a>

                <!-- 二级菜单 -->
                <ul v-if="item.children" class="sub-menu">
                  <li
                    v-for="(child, childIdx) in item.children"
                    :key="childIdx"
                    class="menu-item"
                    :class="{ 'menu-item-has-children': !!child.children }"
                  >
                    <a :href="child.href" @click="handleMenuClick(child, $event)">
                      {{ child.title }}
                    </a>

                    <!-- 三级菜单 -->
                    <ul v-if="child.children" class="sub-menu">
                      <li
                        v-for="(grandChild, gIdx) in child.children"
                        :key="gIdx"
                        class="menu-item"
                      >
                        <a :href="grandChild.href">{{ grandChild.title }}</a>
                      </li>
                    </ul>
                  </li>
                </ul>
              </li>
            </ul>
          </nav>
        </el-col>
<!--        <el-col :span="4" class="text-right">-->
<!--          <a href="/login" class="header_btn thm-btn btn-rounded btn-small">Log In</a>-->
<!--        </el-col>-->
      </el-row>
    </header>

    <!-- Subheader  -->
    <div class="section-bg section-padding subheader">
      <el-container>
        <el-main>
          <div class="container">
            <el-row type="flex" justify="center">
              <el-col :span="24" class="text-center">
                <h1 class="text-white page-title">Contact</h1>
              </el-col>
            </el-row>
          </div>
        </el-main>
      </el-container>
    </div>

    <!-- Contact Section -->
    <!-- Contact Section -->
    <section class="contact-section" style="min-height: 100vh; padding: 40px 20px;">
      <div class="container">
        <el-row :gutter="20" style="margin-bottom: 30px;">
          <!-- 动态渲染每条联系信息 -->
          <el-col v-for="item in contactList" :key="item.id" :span="12" v-show="item.isPublished !== 0">
            <el-card class="info-card" shadow="hover">
              <img :src="fixImgUrl(item.coverImage)" class="contact-icon" />
              <h4>{{ item.title }}</h4>
              <p v-html="item.content"></p>
            </el-card>
          </el-col>
        </el-row>

        <!-- 社交图标 + 地图大框 -->
        <el-card class="map-card" shadow="never" style="background-color: #f5f5f5; padding: 20px;">
          <!-- 社交图标 -->
<!--          <div class="social-links mb-4" style="text-align: center; margin-bottom: 20px;">-->
<!--            <el-link href="#" :underline="false" class="social-icon"><i class="fab fa-facebook-f"></i></el-link>-->
<!--            <el-link href="#" :underline="false" class="social-icon"><i class="fab fa-twitter"></i></el-link>-->
<!--            <el-link href="#" :underline="false" class="social-icon"><i class="fab fa-instagram"></i></el-link>-->
<!--            <el-link href="#" :underline="false" class="social-icon"><i class="fab fa-linkedin-in"></i></el-link>-->
<!--          </div>-->

          <!-- 地图 iframe -->
          <div class="contact-map" style="width: 100%; border-radius: 8px; overflow: hidden;">
            <iframe
              src="https://surl.amap.com/l64WSGl1mcnV"
              width="100%"
              height="600"
              frameborder="0"
            ></iframe>
          </div>
        </el-card>
      </div>
    </section>
  </div>
</template>

<script>
import '@fortawesome/fontawesome-free/css/all.css'
import { listCases } from "@/api/achievement/cases";

export default {
  name: "contact",
  data(){
    return {
      form: {
        name: '',
        email: '',
        phone: '',
        subject: '',
        msg: '',
      },
      contactList: [],
      rules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
        ],
        phone: [{ required: true, message: '请输入电话', trigger: 'blur' }],
        subject: [{ required: true, message: '请输入主题', trigger: 'blur' }],
        msg: [{ required: true, message: '请输入消息内容', trigger: 'blur' }]
      },
      mainMenu: [
        {
          title: '首页',
          href: '/home'
        },
        {
          title: '关于',
          href: '/about'
        },
        {
          title: '服务',
          href: '/services',
        },
        {
          title: '博客',
          href: '#',
          children: [
            {
              title: '博客网络',
              href: '#',
              children: [
                { title: '全宽', href: '/blog-grid' },
                { title: '左侧边栏', href: '/blog-grid-left' },
                { title: '右侧边栏', href: '/blog-grid-right' }
              ]
            },
          ]
        },
        {
          title: '联系我们',
          href: '/contact'
        }
      ]
    };
  },
  created() {
    this.fetchContactInfo(); // 获取联系信息
  },
  methods: {
    fixImgUrl(path) {
      if (!path) return '';
      return path.startsWith(process.env.VUE_APP_BASE_API) ? encodeURI(path) : encodeURI(process.env.VUE_APP_BASE_API + path);
    },
    submitForm() {
      this.$refs.contactForm.validate((valid) => {
        if (valid) {
          // 这里可以提交表单数据
          this.$message.success('表单提交成功！');
          console.log('提交数据:', this.form);
          // 可以调用 API 发送数据
        } else {
          this.$message.error('请正确填写所有字段！');
        }
      });
    },
    async fetchContactInfo() {
      try {
        const query = { type: 'contact' }; // 👈 关键：指定类型
        console.log('请求参数:', query);

        const response = await listCases(query);
        console.log('API 响应:', response);

        if (response && Array.isArray(response.rows)) {
          this.contactList = response.rows;
        } else {
          this.contactList = [];
        }

        console.log('最终渲染数据:', this.contactList);

      } catch (error) {
        console.error("Failed to fetch contact info:", error);
        this.$message.error('加载联系信息失败');
      }
    }
  }
}
</script>

<style scoped>
/* 全局重置与字体 */
#app {
  font-family: 'Poppins', sans-serif;
  margin: 0;
  padding: 0;
}

/* 容器居中，类似 Bootstrap */
.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  box-sizing: border-box;
}

/* =============
   Header 样式
============= */
.header {
  background-color: #ffffff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  padding: 15px 0;
  position: relative;
  z-index: 1000;
}

.header.can-sticky {
  position: sticky;
  top: 0;
  transition: all 0.3s ease;
}

.logo img {
  height: 50px;
  transition: all 0.3s ease;
}

.logo img:hover {
  opacity: 0.9;
}

/* 导航栏 - 主菜单 */
.navigation {
  display: flex;
  justify-content: center;
}

.main-menu {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  gap: 30px;
  align-items: center;
}

.main-menu > .menu-item {
  position: relative;
}

.main-menu > .menu-item > a {
  text-decoration: none;
  color: #333;
  font-weight: 500;
  font-size: 16px;
  transition: color 0.3s ease;
  padding: 5px 0;
  display: block;
}

.main-menu > .menu-item > a:hover,
.main-menu > .menu-item > a.active {
  color: #0066ff;
}

/* 二级菜单 */
.sub-menu {
  position: absolute;
  top: 100%;
  left: 0;
  background: #fff;
  min-width: 200px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
  opacity: 0;
  visibility: hidden;
  transform: translateY(-10px);
  transition: all 0.3s ease;
  list-style: none;
  margin: 0;
  padding: 10px 0;
  z-index: 1001;
}

.menu-item-has-children:hover > .sub-menu {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.sub-menu .menu-item {
  padding: 0;
}

.sub-menu .menu-item > a {
  color: #555;
  padding: 10px 20px;
  font-size: 15px;
  display: block;
  transition: background 0.3s ease, color 0.3s ease;
}

.sub-menu .menu-item > a:hover {
  background-color: #f5f9ff;
  color: #0066ff;
  padding-left: 24px;
}

/* 三级菜单 */
.sub-menu .sub-menu {
  left: 100%;
  top: 5px;
}

/* 登录按钮 */
.header_btn {
  background-color: #0066ff;
  color: #fff !important;
  padding: 10px 20px;
  text-decoration: none;
  border-radius: 30px;
  font-size: 14px;
  font-weight: 600;
  transition: all 0.3s ease;
  display: inline-block;
  border: none;
  outline: none;
}

.header_btn:hover {
  background-color: #0052cc;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 102, 255, 0.3);
}

/* =============
   Subheader 样式
============= */
.subheader {
  background-image: url('https://images.unsplash.com/photo-1454165804606-c3d57bc86b40?ixlib=rb-4.0.3&auto=format&fit=crop&w=1350&q=80');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  padding: 100px 0;
  color: #fff;
  position: relative;
}

.subheader::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5); /* 深色遮罩提升文字可读性 */
  z-index: 1;
}

.subheader .el-main {
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
}

.page-title {
  font-size: 3rem;
  font-weight: 700;
  margin: 0;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
  letter-spacing: -0.5px;
}

.text-white {
  color: #ffffff;
}

.text-center {
  text-align: center;
}

/* =============
   响应式设计
============= */
@media (max-width: 992px) {
  .main-menu {
    gap: 20px;
  }

  .page-title {
    font-size: 2.5rem;
  }
}

@media (max-width: 768px) {
  .header {
    padding: 12px 0;
  }

  .logo img {
    height: 40px;
  }

  .main-menu {
    gap: 15px;
    flex-wrap: wrap;
    justify-content: center;
  }

  .main-menu > .menu-item {
    font-size: 14px;
  }

  .sub-menu {
    min-width: 180px;
  }

  .sub-menu .menu-item > a {
    padding: 8px 16px;
    font-size: 14px;
  }

  .page-title {
    font-size: 2rem;
  }

  .container {
    padding: 0 15px;
  }
}

@media (max-width: 576px) {
  .main-menu {
    gap: 10px;
    font-size: 14px;
  }

  .header_btn {
    padding: 8px 16px;
    font-size: 13px;
  }

  .page-title {
    font-size: 1.8rem;
  }
}

/* 标题样式 */
.section-header .title {
  font-size: 24px;
  font-weight: 600;
  color: #ffffff;
  margin-bottom: 10px;
}

/* 卡片统一样式 */
.info-card {
  text-align: center;
  padding: 20px;
  transition: all 0.3s ease;
  background-color: #f9f9f9;
  min-height: 200px; /* 设置最小高度 */
  display: flex;
  flex-direction: column;
  justify-content: center; /* 内容垂直居中 */
}

.info-card h4 {
  font-size: 16px;
  color: #333;
  margin: 10px 0;
}

.info-card p {
  font-size: 14px;
  color: #666;
  line-height: 1.6;
  margin: 0;
}

.info-card i {
  font-size: 28px;
  color: #0066ff;
  margin-bottom: 10px;
}

/* 社交图标样式 */
.social-links {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 20px;
  color: black;
}

.social-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  font-size: 18px;
  transition: all 0.3s;
  color: black;
}

.social-icon:hover {
  background-color: #0066ff;
  color: black;
  border-color: #0066ff;
  transform: translateY(-3px);
}

/* 地图卡片 */
.map-card {
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0,0,0,0.08);
}

.contact-info-card :deep(.el-card__body) {
  padding: 20px;
  height: 100%;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
}

.text-white {
  color: #fff;
}

/* 联系方式列表 */
.contact-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.contact-item {
  display: flex;
  align-items: center;
  color: #fff;
  margin-bottom: 16px;
  font-size: 15px;
}

.contact-item i {
  margin-right: 10px;
  color: #fff;
}

/* 社交图标 */
.social-links {
  display: flex;
  justify-content: center;
  gap: 12px;
}

.social-icon {
  color: #ffffff;
  font-size: 18px;
  width: 36px;
  height: 36px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.social-icon:hover {
  background-color: rgba(255, 255, 255, 0.1);
  transform: translateY(-2px);
}

.contact-map iframe {
  border-radius: 8px;
}

/* 提交按钮样式 */
.btn-rounded {
  border-radius: 30px;
}

.w-100 {
  width: 100%;
}

.mb-4 {
  margin-bottom: 1.5rem;
}

/* 响应式 */
@media (max-width: 992px) {
  .contact-info-card {
    margin-top: 20px;
  }
}
.contact-icon {
  width: 48px;
  height: 48px;
  object-fit: contain; /* 确保图片完整显示 */
  margin: 0 auto 12px;
  display: block;
}

.info-card {
  text-align: center;
  padding: 20px;
}
.info-card i {
  font-size: 40px;
  color: #409EFF;
  margin-bottom: 10px;
}
.info-card img {
  width: 40px;
  height: 40px;
  margin-bottom: 10px;
}
.info-card h4 {
  margin: 10px 0;
  font-size: 18px;
  color: #333;
}
.info-card p {
  color: #666;
  line-height: 1.6;
}
.icon-primary {
  color: #409EFF;
}
</style>
